package curves;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * A class  to represent the variations of some function  in some interval.
 * Allows some control on the accuracy of this representation
 */

/**
 * @author casteran
 */
public class CurveFrame extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** the graphic part of the display */
	protected CurveTracerUser tracer;

	/** information bar */
	protected CurveInfosUser infos;

	/** various commands */
	protected CurveControlsUser controls;

	protected double old_x = 0;
	/**
	 * Builds a top-level window from the variations of a function
	 * 
	 * @see FunctionVariations
	 */
	public CurveFrame(FunctionVariations fvar, String var_name) {
		super("Curve");
		
		tracer = new CurveTracerUser(fvar, var_name);
		infos = new CurveInfosUser(fvar, tracer);
		controls = new CurveControlsUser(fvar, this);

		JPanel mainPane = new JPanel(new BorderLayout());

		mainPane.add(tracer, BorderLayout.CENTER);
		mainPane.add(infos, BorderLayout.SOUTH);
		mainPane.add(controls, BorderLayout.EAST);

		
		fvar.tabulate(controls.currentPrecision());
		
		tracer.addMouseListener(new MouseAdapter() {
			CurveFrame cf = CurveFrame.this;

			public void mouseEntered(MouseEvent e) {
				cf.infos.xmouse.setText("x = " + cf.tracer.realX(e.getX()));
				cf.infos.ymouse.setText("y = " + cf.tracer.realY(e.getY()));
				cf.infos.repaint();
			}

			public void mouseExited(MouseEvent e) {
				cf.infos.xmouse.setText("");
				cf.infos.ymouse.setText("");
				cf.infos.repaint();
			}
			
			public void mouseReleased(MouseEvent e) {
				old_x = 0;
			}
			
		});
		
		tracer.addMouseMotionListener(new MouseMotionAdapter() {
			CurveFrame cf = CurveFrame.this;
			double pas = 0;

			public void mouseMoved(MouseEvent e) {
				cf.infos.xmouse.setText("x = " + cf.tracer.realX(e.getX()));
				cf.infos.ymouse.setText("y = " + cf.tracer.realY(e.getY()));
				cf.infos.repaint();
			}

			public void mouseDragged(MouseEvent e) {
				
				cf.infos.xmouse.setText("x = " + cf.tracer.realX(e.getX()));
				cf.infos.ymouse.setText("y = " + cf.tracer.realY(e.getY()));
				
				 //-- DRAGING
                if(old_x == 0)
                        old_x = cf.tracer.realX(e.getX());
                
                pas = old_x - cf.tracer.realX(e.getX());
                
                cf.tracer.setXmin(cf.tracer.getXmin() + pas);
                cf.tracer.setXmax(cf.tracer.getXmax() + pas);

                cf.tracer.tabulateThemAll(cf.controls.currentPrecision());
                cf.tracer.repaint();
                
                old_x = cf.tracer.realX(e.getX()); 
                // END DRAGGING

					
				
				
				cf.infos.repaint();
			}
		});

		
		
		setContentPane(mainPane);
		pack();
		infos.update();
		setVisible(true);
	}
	
	//ADDED
	
	public CurveTracerUser getTracer(){
		return tracer;
	}
	
	public CurveControlsUser getControls(){
		return controls;
	}
	
	//END ADDED
}
